package com.aplink.generic.google.maps.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.TextView;

public class SquareTextView extends TextView {
	private int mOffsetLeft = 0;
	private int mOffsetTop = 0;

	public SquareTextView(final Context context) {
		super(context);
	}

	public SquareTextView(final Context context, final AttributeSet attrs) {
		super(context, attrs);
	}

	public SquareTextView(final Context context, final AttributeSet attrs,
			final int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	public void draw(final Canvas canvas) {
		canvas.translate(mOffsetLeft / 2, mOffsetTop / 2);
		super.draw(canvas);
	}

	@Override
	protected void onMeasure(final int widthMeasureSpec,
			final int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		final int width = getMeasuredWidth();
		final int height = getMeasuredHeight();
		final int dimension = Math.max(width, height);
		if (width > height) {
			mOffsetTop = width - height;
			mOffsetLeft = 0;
		} else {
			mOffsetTop = 0;
			mOffsetLeft = height - width;
		}
		setMeasuredDimension(dimension, dimension);
	}
}
